<?php
class Helper_Security
{
	//共用属性
	public $IsAdmin = false;
	public $position = 0;

	//前台用户属性
	public $typecode;
	public $logincode;
	public $tempcode;

	//后台管理员属性
	public $loginname;
	public $issuper;
	public $lastlogindate;

	//new
	public $Account_id;
	public $Account_Username;

	public $Member_id;
	public $Member_Nickname;
	public $Member_Privilege;

	public $Company_id;
	public $Company_Name;
	
	public $Language_id;
	public $Language_FileName;
	public $Language_NativeName;
	
	public $Type;
	public $Identification;

	function __construct()
	{
		if(isset($_SESSION["Admin"])) {
			$this->Admin_id			= $_SESSION["Admin"]["id"];
			$this->Admin_Username	= $_SESSION["Admin"]["Username"];
			$this->Admin_Name		= $_SESSION["Admin"]["Name"];
		}

		if(isset($_SESSION["member_IsAdmin"])) {
			$this->IsAdmin			= $_SESSION["member_IsAdmin"];
		}

//		$this->typecode			= ($_SESSION["member_iTypeCode"] == "")? 3096011:intval($_SESSION["member_iTypeCode"]);
//		$this->logincode		= ($_SESSION["member_iLoginCode"] == "")? 3096011:$_SESSION["member_iLoginCode"];
//		$this->tempcode			= ($_SESSION["member_TempCode"] == "")? "a":$_SESSION["member_TempCode"];
//		//2008-10-29
//		$this->loginname		= ($_SESSION["member_cLoginName"] == "")?1:$_SESSION["member_cLoginName"];
//		$this->issuper			= ($_SESSION["member_iIsSuperManager"] == "")?1:$_SESSION["member_iIsSuperManager"];
//		$this->lastlogindate	= ($_SESSION["member_dLastLoginDate"] == "")?1:$_SESSION["member_dLastLoginDate"];

		if(isset($_SESSION["COMPANY"])) {
			$this->Company_id		= ($_SESSION["COMPANY"]["id"] == "")?0:$_SESSION["COMPANY"]["id"];
			$this->Company_Name		= ($_SESSION["COMPANY"]["Name"] == "")?'':$_SESSION["COMPANY"]["Name"];
		}
		
		if(isset($_SESSION["ACCOUNT"])) {
			$this->Account_id		= ($_SESSION["ACCOUNT"]["id"] == "")?0:$_SESSION["ACCOUNT"]["id"];
			$this->Account_Username	= ($_SESSION["ACCOUNT"]["Username"] == "")?1:$_SESSION["ACCOUNT"]["Username"];
		}
		if(isset($_SESSION["MEMBER"])) {
			$this->Member_id		= ($_SESSION["MEMBER"]["id"] == "")?0:$_SESSION["MEMBER"]["id"];
			$this->Member_Nickname	= ($_SESSION["MEMBER"]["Nickname"] == "")?'':$_SESSION["MEMBER"]["Nickname"];
			$this->Member_Privilege	= (!isset($_SESSION["MEMBER"]["Privilege"]) || $_SESSION["MEMBER"]["Privilege"] == "")?'':$_SESSION["MEMBER"]["Privilege"];
			$this->Member_Role		= (!isset($_SESSION["MEMBER"]["Role"]) || $_SESSION["MEMBER"]["Role"] == "")?'':$_SESSION["MEMBER"]["Role"];
			$this->Member_AgentID	= (!isset($_SESSION["MEMBER"]["AgentID"]) || $_SESSION["MEMBER"]["AgentID"] == "")?'':$_SESSION["MEMBER"]["AgentID"];
			$this->Member_ParentID	= (!isset($_SESSION["MEMBER"]["ParentID"]) || $_SESSION["MEMBER"]["ParentID"] == "")?'':$_SESSION["MEMBER"]["ParentID"];
			$this->Member_FirstName	= (!isset($_SESSION["MEMBER"]["FirstName"]) || $_SESSION["MEMBER"]["FirstName"] == "")?'':$_SESSION["MEMBER"]["FirstName"];
			$this->Member_LastName	= (!isset($_SESSION["MEMBER"]["LastName"]) || $_SESSION["MEMBER"]["LastName"] == "")?'':$_SESSION["MEMBER"]["LastName"];
		}
		
		if(isset($_SESSION["LANGUAGE"])) {
			$this->Language_id				= ($_SESSION["LANGUAGE"]["id"] == "")?0:$_SESSION["LANGUAGE"]["id"];
			$this->Language_FileName		= ($_SESSION["LANGUAGE"]["FileName"] == "")?'':$_SESSION["LANGUAGE"]["FileName"];
			$this->Language_NativeName		= ($_SESSION["LANGUAGE"]["NativeName"] == "")?'':$_SESSION["LANGUAGE"]["NativeName"];
		}
	}
	
	function page($type)
	{
		define('WEB_POSITION', $type);
		
		switch( $type ){
			case PAGE_BACK:
				$this->LoginValidate();
				break;
		}
	}

	function privilege( $module_code_or_nickname, $function_code_or_nickname="" )
	{
		foreach($this->privileges as $privilege){
			if( $function_code_or_nickname == "")
			{
				if(in_array($module_code_or_nickname, $privilege) ){
					return true;
				}
			} else {
				if(in_array($module_code_or_nickname, $privilege) && in_array($function_code_or_nickname, $privilege)){
					return true;
				}
			}
		}

		return false;
	}

	function validatePrivilege( $module_code_or_nickname, $function_code_or_nickname )
	{
		if( $this->privilege( $module_code_or_nickname, $function_code_or_nickname ) == false){
			htmlError("No Privilege!", "/admin/index.php", 5);
		}
	}

	/**
	 * 退出登录
	 *
	 */
	public function LogOut()
	{
		unset($_SESSION["Admin"]);
		unset($_SESSION["ACCOUNT"]);
		unset($_SESSION["MEMBER"]);
		unset($_SESSION["COMPANY"]);
		unset($_SESSION["LANGUAGE"]);

		htmlSuccess("Logged out!", "/", 5);
	}
	
	/**
	 * 判断登录，如未登录则自动转到登录页面
	 *
	 */
	public function LoginValidate()
	{
		if( $this->LoginValidation() == false )
		{
			$_SESSION["required_url"] = $_SERVER["REQUEST_URI"];

			$this->LogOut();
		}
	}

	/**
	 * 返回登录状态
	 *
	 * @return boolean
	 */
	public function LoginValidation()
	{
		$returnValue = false;
		// defined('WEB_POSITION') == true &&
		if( $this->Account_id != 0  ){
			$returnValue = true;
		}

		return $returnValue;
	}

	/**
	 * 检查权限
	 *
	 * @param string $module : 模块名称,模块间以,分隔
	 * @param boolean $modify : 是否要求有修改(添加、编辑、删除)权限
	 * @return boolean
	 */
	function CheckPowerNoMsg($module)
	{
		if($this->issuper)
		{
			return true;
		}
		if( strlen($module) > 6){
			return true;
		}
		if($module)
		{
			if( preg_match( "/(^|,)".$module."($|,)/", $this->privileges) == true)
			{
				return true;
			}
		}
		return false;
	}

	function CheckPower($module)
	{
		if($this->CheckPowerNoMsg($module))
		{
			return true;
		} else {
			$this->PowerErrorMsg();
			return false;
		}
	}

	/**
	 * 没有操作权限时的提示文字
	 *
	 */
	function PowerErrorMsg()
	{
		ErrorMsg("没有操作权限");
	}
}
?>